package com.xiaoxi.bms.controller.log;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.common.util.ExcelUtils;
import com.xiaoxi.bms.domain.log.BMSLogLogin;
import com.xiaoxi.bms.domain.log.dto.BMSLogLoginDTO;
import com.xiaoxi.bms.domain.log.vo.BMSLogLoginVO;
import com.xiaoxi.bms.service.common.circulation.ExportDataToExcelService;
import com.xiaoxi.bms.service.log.BmsLogLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName BmsLogLoginController
 * @Description
 * @Author xiaoxi
 * @Date 2022-10-31
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/log/api/v1/login")
public class BmsLogLoginController {

    @Resource
    private BmsLogLoginService bmsLogLoginService;

    @Resource(name = "bmsLogLoginServiceImpl")
    private ExportDataToExcelService exportDataToExcelService;

    /**
     * 查询分页数据
     *
     * @param bmsLogLoginDTO
     * @param pageable
     * @return
     */
    @GetMapping("/findListByPage")
    public ApiResponse findListByPage(BMSLogLoginDTO bmsLogLoginDTO, Pageable pageable) {
        try {
            IPage<BMSLogLoginVO> result = bmsLogLoginService.findListByPage(bmsLogLoginDTO, pageable);
            return ApiResponse.ofSuccess(result);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 导出登录日志
     *
     * @param bmsLogLoginDTO 查询条件
     * @return
     */
    @GetMapping("exportLoginLog")
    public void exportUserInfo(BMSLogLoginDTO bmsLogLoginDTO, HttpServletRequest request, HttpServletResponse response) {

        try {
            ExcelUtils.exportExcel(request, response, "登录日志详情表.xls", exportDataToExcelService, bmsLogLoginDTO);
        } catch (BMSException e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("addLogLogin")
    public ApiResponse addLogLogin(@RequestBody BMSLogLogin bmsLogLogin) {

        try {
            bmsLogLoginService.add(bmsLogLogin);
        } catch (BMSException e) {
            log.error("数据类型有误：{} --- 未记录登录日志成功 : {}", e.getMessage(), bmsLogLogin.toString());
            return ApiResponse.ofException(e);
        }
        return ApiResponse.ofSuccess();
    }

}
